package com.cjw.service.impl.systemManage;

import com.cjw.enums.share.PlanState;
import com.cjw.enums.systemManage.ExamineState;
import com.cjw.enums.systemManage.ExamineType;
import com.cjw.mapper.livesquare.PlanMapper;
import com.cjw.mapper.systemManage.ExamineMapper;
import com.cjw.pojo.dto.database.PageDTO;
import com.cjw.pojo.dto.database.UserDTO;
import com.cjw.pojo.dto.systemManage.ExamineDTO;
import com.cjw.pojo.dto.systemManage.PassDTO;
import com.cjw.pojo.entity.livesquare.Plan;
import com.cjw.pojo.entity.systemManage.Examine;
import com.cjw.pojo.query.systemManage.ExamineQuery;
import com.cjw.service.person.MyDesignService;
import com.cjw.service.systemManage.ExamineService;
import com.cjw.util.BaseContextHolder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : cjw
 * @date: : 2022/2/27 9:58
 * @description : ExamineServiceImpl
 */
@Service("examineService")
public class ExamineServiceImpl implements ExamineService {

    @Autowired
    private ExamineMapper examineMapper;

    @Autowired
    private MyDesignService myDesignService;

    @Autowired
    private PlanMapper planMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addExamineByPerson(Integer type, Long contentId, String content) {
        return addExamine(type, contentId, content, ExamineState.WAIT.getCode());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addExamineByAuto(Integer type, Long contentId, String content) {
        addExamine(type, contentId, content, ExamineState.UNPASS.getCode());
    }

    @Override
    public PageDTO<List<ExamineDTO>> findListExamine(ExamineQuery examineQuery) {
        PageDTO<List<ExamineDTO>> listPageDTO = new PageDTO<>();
        ArrayList<ExamineDTO> examineDTOS = new ArrayList<>();
        PageHelper.startPage(examineQuery.getPage(),examineQuery.getSize());
        Example example = new Example(Examine.class);
        Example.Criteria criteria = example.createCriteria();
        if (examineQuery.getState() != null )
            criteria.andEqualTo("state",examineQuery.getState());
        if (examineQuery.getType() != null)
            criteria.andEqualTo("type", examineQuery.getType());
        if (examineQuery.getContent() != null && !examineQuery.getContent().trim().equals(""))
            criteria.andLike("content", "%"+examineQuery.getContent()+"%");
        example.setOrderByClause("id desc");
        PageInfo<Examine> examinePageInfo = new PageInfo<>(examineMapper.selectByExample(example));
        listPageDTO.setTotal((int)examinePageInfo.getTotal());
        for (Examine examine : examinePageInfo.getList()) {
            ExamineDTO examineDTO = new ExamineDTO();
            examineDTO.setAddTime(examine.getAddTime());
            examineDTO.setContent(examine.getContent());
            examineDTO.setId(examine.getId());
            examineDTO.changeState(examine.getState());
            examineDTO.changeType(examine.getType());
            examineDTO.setAdminId(examine.getAdminId());
            examineDTO.setRemark(examine.getRemark());
            examineDTOS.add(examineDTO);
        }
        listPageDTO.setDtos(examineDTOS);
        return listPageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean pass(PassDTO passDTO) {
        return updateExamine(passDTO, ExamineState.PASS.getCode());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean unpass(PassDTO passDTO) {
        if (!updateExamine(passDTO, ExamineState.UNPASS.getCode()))
            return false;
        // 通过类型去删除
        Examine examineSelect = examineMapper.selectByPrimaryKey(passDTO.getId());
        // 删除审核没有通过的内容
        if (examineSelect.getType() == ExamineType.LIVE.getCode()) {
            myDesignService.deleteLive(examineSelect.getContentId());
        } else if (examineSelect.getType() == ExamineType.PLAN.getCode()) {
            Plan plan = new Plan();
            plan.setId(examineSelect.getContentId());
            plan.setState(PlanState.CANCEL.getCode());
            planMapper.updateByPrimaryKeySelective(plan);
        } else if (examineSelect.getType() == ExamineType.READ.getCode()) {
            myDesignService.deleteRead(examineSelect.getContentId());
        }
        return true;
    }

    private Boolean updateExamine(PassDTO passDTO, Integer state) {
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        Examine examine = new Examine();
        examine.setRemark(passDTO.getRemark());
        examine.setId(passDTO.getId());
        examine.setRemark(passDTO.getRemark());
        examine.setState(state);
        examine.setAdminId(user.getId());
        examine.setUpdateTime(new Date());
        return examineMapper.updateByPrimaryKeySelective(examine) == 1;
    }

    private Boolean addExamine(Integer type, Long contentId, String content, Integer state) {
        Examine examineFind = new Examine();
        examineFind.setType(type);
        examineFind.setContentId(contentId);
        if (examineMapper.selectCount(examineFind) != 0) {
            // 如果记录表已存在，那么进行更新
            examineFind.setAddTime(new Date());
            examineFind.setContent(content);
            return examineMapper.updateByPrimaryKeySelective(examineFind) == 1;
        } else {
            // 否则进行添加
            return examineMapper.insert(buildExamine(type, contentId, content, state)) == 1;
        }
    }

    /**
     * 构建Examine实体
     * @return
     */
    private Examine buildExamine(Integer type, Long contentId, String content, Integer state) {
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        Examine examine = new Examine();
        examine.setAddTime(new Date());
        examine.setContent(content);
        examine.setContentId(contentId);
        examine.setState(state);
        examine.setType(type);
        examine.setUserId(user.getId());
        return examine;
    }
}
